package com.wskj.shiro.chapter2.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;
import org.w3c.dom.html.HTMLTableRowElement;

public class MyRealm2 implements Realm {

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "myRealm2";
	}

	@Override
	public boolean supports(AuthenticationToken token) {
		// TODO Auto-generated method stub
		return token instanceof UsernamePasswordToken;
	}

	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		String  username=(String)token.getPrincipal(); //用户名
		//密码
		String  password=new String (  (char[])token.getCredentials());
		
		//账户错误
		if(!"lisi".equals(username)) {
			throw new UnknownAccountException();
		}
		
		//密码错误
		if(!"456".equals(password)) {
			throw  new IncorrectCredentialsException();
		}
		
		
		return new SimpleAuthenticationInfo(username,password,getName());
	}

}
